﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TI;
using MongoDB;

namespace TI_Test
{
    public class AttachmentLoader
    {
        private  static List<AttachItem> loadList;
        MergeTiConfig config = null;
        
        public static void PopulateLoadList()
        {
            loadList = new List<AttachItem>();
            Mongo mongo = new Mongo();
            IMongoDatabase db = mongo.GetDatabase("TI");
            string script = string.Empty;
            string currrentID = string.Empty;            
            try
            {
              mongo.Connect();
              Document r = db.Eval("getNotLoadAttachList(2);");
              List<Document> lst = (List<Document>)r["retval"];
              if (null == lst)
                  return ;

              foreach (Document d in lst)
              {
                loadList .Add(Helper.Json<AttachItem>.Deserialize(d.ToString()));              
              }

              foreach (AttachItemRecord i in loadList[0].value)
              {
                  UpdateAttachItem(i);             
              }
             }
            
            finally {
                mongo.Disconnect();
            }
        }

        public static void UpdateAttachItem(AttachItemRecord itm) {
            Mongo mongo = new Mongo();
            IMongoDatabase db = mongo.GetDatabase("TI");
            try{
                mongo.Connect ();
                db.Eval ("updateDocumentMetaItem("+itm.ToString ()+")");
            }
            finally {
                mongo.Disconnect ();
            }
        }
    }

    
}
